"""
权限管理
"""
import logging

import pytest
from Config.Base import Base
from playwright.sync_api import Page, expect, Playwright
from Config.config import *
from Config.URL import *
import allure, os
from Config.Base import Base
from Config.Tool import tool


@pytest.mark.基础管理系统
@pytest.mark.组织权限
#@pytest.mark.parametrize('case', case_data)
def test_case(playwright : Playwright) -> None:
    SetupTeardown = Base(playwright)
    SetupTeardown.goto(url)
    SetupTeardown.login(username_admin, password_admin)
    context = SetupTeardown.context
    page = SetupTeardown.page
    page.goto(url+url_jichu_jueseguanli)
    page.get_by_placeholder("请输入用户名").fill(username_admin)
    page.get_by_placeholder("请输入密码").fill(password_admin)
    page.get_by_role("button", name="登 录").click()
    page.get_by_text("UAT-环北部湾广西水资源配置工程").first.click()
    page.get_by_text("组织权限 组织权限").click()
    page.get_by_role("link", name="ꘙ 角色管理").click()
    page.get_by_role("button", name=" 新建").click()
    page.get_by_label("新建").get_by_placeholder("请选择角色类型").click()
    page.get_by_role("listitem").nth(1).click()
    page.get_by_placeholder("请输入角色名称").fill("自动化测试角色名称1")
    page.get_by_placeholder("请输入角色编码").fill("JSBM1")
    page.get_by_label("新建").get_by_role("spinbutton").fill("01")
    page.get_by_role("button", name="确 定").click()
    expect(page.get_by_role("paragraph")).to_contain_text("新建成功！")
    page.wait_for_timeout(1000)
    page.goto(url + url_jichu_quanxianguanli)
    page.get_by_role("treeitem", name=" 򡘵 我的项目").locator("span").nth(2).click()
    page.locator("div").filter(has_text=re.compile(r"^组织权限$")).locator("span").nth(2).click()
    page.wait_for_timeout(1000)
    page.get_by_role("button", name="下一步").click()
    page.wait_for_timeout(1000)
    page.get_by_role("tree").locator("div").filter(has_text=re.compile(r"^我的项目$")).locator("span").nth(2).click()
    page.locator("div").filter(has_text=re.compile(r"^组织权限$")).locator("span").nth(2).click()
    page.wait_for_timeout(1000)

    page.get_by_role("button", name="下一步").click()
    page.wait_for_timeout(1000)
    page.get_by_role("treeitem", name=" 自动化测试角色名称1").locator("span").nth(2).click()
    page.get_by_role("button", name="确 定").click()
    expect(page.get_by_role("paragraph")).to_contain_text("操作成功")

    page.goto(url+url_yonghuguanli)
    page.get_by_role("tree").get_by_text("环北部湾广西水资源配置有限公司").click()
    page.get_by_role("button", name=" 新建").click()
    page.get_by_placeholder("请输入账户").fill('zicequanxian')
    page.locator("form").filter(has_text="基础信息账户").get_by_placeholder("请输入姓名").fill('AutoTest权限用户')
    page.get_by_placeholder("请选择性别").click()
    page.locator("li").filter(has_text="女").click()
    page.locator(".el-tag__close").click()
    page.locator(".el-select__input").click()
    page.get_by_text("自动化测试角色名称").click()
    page.get_by_role("button", name="确 定").click()
    # 断言
    expect(page.get_by_text('新建成功！')).to_be_visible()

    # 测试结果截图
    filename = os.path.join(test_screenshot_dir, f"{'权限管理'}.png")
    page.screenshot(path=filename)
    allure.attach.file(source=filename, name='权限管理',
                       attachment_type=allure.attachment_type.PNG)



    page.goto(url+url_jichu_jueseguanli)
    page.get_by_placeholder("请输入名称").fill("自动化测试角色名称1")
    page.get_by_role("textbox", name="请选择角色类型").click()
    page.get_by_role("listitem").nth(1).click()
    page.get_by_role("button", name=" 查询").click()
    # 获取tbody下面元素tr个数
    expect(page.locator("tbody > tr")).to_have_count(1)
    # 获取是否存在文本text
    expect(page.locator("tbody")).to_contain_text("自动化测试角色名称1")

    page.get_by_role("button", name="删除").click()
    page.get_by_role("button", name="确 定").click()
    expect(page.get_by_role("alert")).to_contain_text("删除成功！")

    page.goto(url+url_yonghuguanli)
    page.get_by_placeholder("请输入账号").fill('zicequanxian')
    page.get_by_role("button", name=" 查询").click()
    # 获取tbody下面元素tr个数
    expect(page.locator("tbody > tr")).to_have_count(1)
    # 获取是否存在文本text
    expect(page.locator("tbody")).to_contain_text('zicequanxian')

    page.get_by_role("button", name="删除").click()
    page.get_by_role("button", name="确 定").click()
    expect(page.get_by_text('删除成功！')).to_be_visible()

    context.close()
    SetupTeardown.close()


